package tripleo.amemu;

import java.sql.*;

public class ds1 implements ds {
	private  PreparedStatement lookup_prep;
	private  PreparedStatement delete_prep;
	private  PreparedStatement enter_prep;

	static {
		try {
			Class.forName("org.hsqldb.jdbcDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		}
	}

	/* (non-Javadoc)
	 * @see tripleo.amemu.ds#mkdb()
	 */
	public void mkdb() throws ds_exception {
		try {
			System.out.println("Re-creating the database...");

			Statement stat = conn().createStatement();

			try {
				stat.executeUpdate("DROP TABLE objcaps");
			} catch (SQLException e) {
			}

			stat.execute("CREATE TABLE objcaps(cap integer,name varchar(255))");
			stat.close();
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see tripleo.amemu.ds#enter(java.lang.String, int)
	 */
	public boolean enter(String name, int cap) throws ds_exception {
		try {
			enter_prep.setString(2, name);
			enter_prep.setInt(1, cap);
			enter_prep.executeUpdate();
//			enter_prep.close();
			return true; // TODO
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see tripleo.amemu.ds#delete(int)
	 */
	public void delete(int cap) throws ds_exception {
		try {
			delete_prep.setInt(1, cap);
			delete_prep.executeUpdate();
//			delete_prep.close();
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see tripleo.amemu.ds#lookup(java.lang.String)
	 */
	public int lookup(String aName) throws ds_exception {
		try {

			lookup_prep.setString(1, aName);
			ResultSet e = lookup_prep.executeQuery();

			int n = 0;
			int rcap = -1;
			while (e.next()) {
				n++;
				rcap = e.getInt(1);//cap
			}

			switch (n) {
				case 0:  throw new ds_exception("NOT_FOUND");
				case 1:  break;
				default: throw new ds_exception("INTERNAL");
			}

			e.close();
//			lookup_prep.close();
			return rcap;
		} catch (SQLException e1) {
			throw new ds_exception(e1);
		}
	}

	/* (non-Javadoc)
	 * @see tripleo.amemu.ds#close()
	 */
	public void close() throws ds_exception {
		try {
			conn.close();
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
	}

	public Connection conn() {
		return conn;
	}

	public ds1(String aPath) throws ds_exception {
		String _path = aPath;
		Connection _conn;
		final String P = _path+"/"+_path;
		try {
			_conn = DriverManager.getConnection("jdbc:hsqldb:" + P, "sa", "");
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
		_init(P, _conn);
	}

	public ds1(String aPath, Connection aConn) throws ds_exception {
		_init(aPath, aConn);
	}

	private void _init(String aPath, Connection aConn) throws ds_exception {
		path = aPath;
		conn = aConn;
		//
	}

	/**
	 * @throws ds_exception
	 */
	public void start() throws ds_exception {
		try {
			lookup_prep = conn().prepareCall("SELECT CAP FROM objcaps WHERE name=?");
			delete_prep = conn().prepareCall("DELETE FROM objcaps WHERE cap=?");
			enter_prep  = conn().prepareCall("INSERT INTO objcaps (cap,name) VALUES (?,?)");
		} catch (SQLException e) {
			throw new ds_exception(e);
		}
	}

	String path;
	Connection conn;


}